<form class="layui-form model-form" id="form-password">
    <div class="layui-form-item">
        <label class="layui-form-label">原始密码:</label>
        <div class="layui-input-block">
            <input type="password" name="oldPassword" placeholder="请输入原始密码" class="layui-input"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新密码:</label>
        <div class="layui-input-block">
            <input type="password" name="newPassword" placeholder="请输入新密码" class="layui-input"
                   lay-verType="tips" lay-verify="required|password" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">确认密码:</label>
        <div class="layui-input-block">
            <input type="password" name="rePassword" placeholder="请再次输入新密码" class="layui-input"
                   lay-verType="tips" lay-verify="required|repassword" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="submit-password" lay-submit>保存</button>
        </div>
    </div>
</form>

<script type="text/javascript" src="assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="assets/libs/jquery/jquery.md5.js"></script>
<script>
    layui.use(['layer', 'form','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;

        // 监听提交
        form.on('submit(submit-password)', function (data) {
            data.field.oldPassword = $.md5(data.field.oldPassword);
            data.field.newPassword = $.md5(data.field.newPassword);
            admin.req('user/admin/login/updatePassword', data.field, function (data) {
                if (data.code == 200) {
                    layer.closeAll();
                    layer.msg(data.msg, {icon: 1});
                    setTimeout(function () {
                        location.replace('/admin/login.html');
                    }, 300);
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            }, 'PUT');
            return false;
        });

        // 添加表单验证方法
        form.verify({
            password: [/^[\S]{5,24}$/, '密码必须5到24位，且不能出现空格'],
            repassword: function (t) {
                if (t !== $('#form-password input[name=newPassword]').val()) {
                    return '两次密码输入不一致';
                }
            }
        });

    });
</script>